import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/HomePage.vue'),
    meta: {
      title: '首页 - 三角洲数据查询攻略站'
    }
  },
  {
    path: '/items',
    name: 'items',
    component: () => import('@/views/ItemList/index.vue'),
    meta: {
      title: '物品列表 - 三角洲数据查询攻略站'
    }
  },
  {
    path: '/ranking',
    name: 'ranking',
    component: () => import('@/views/Ranking/index.vue'),
    meta: {
      title: '排行榜 - 三角洲数据查询攻略站'
    }
  },
  {
    path: '/creators',
    name: 'creators',
    component: () => import('@/views/Creators/index.vue'),
    meta: {
      title: '内容创作者 - 三角洲数据查询攻略站'
    }
  },
  {
    path: '/tactical-loadouts',
    name: 'tactical-loadouts',
    component: () => import('@/views/TacticalLoadouts/index.vue'),
    meta: {
      title: '战术装备推荐 - 三角洲数据查询攻略站'
    }
  },
  // 任务流程图路由
  {
    path: '/task-flowchart',
    name: 'task-flowchart',
    component: () => import('@/views/TaskFlowchart/index.vue'),
    meta: {
      title: '任务流程图 - 三角洲数据查询攻略站'
    }
  },
  // 地图相关路由
  {
    path: '/maps/prison',
    name: 'prison-map',
    component: () => import('@/views/Maps/PrisonMap.vue'),
    meta: {
      title: '监狱地图 - 三角洲数据查询攻略站'
    }
  },
  // 功能请求相关路由
  {
    path: '/feature',
    name: 'feature-requests',
    component: () => import('@/views/Feature/index.vue'),
    meta: {
      title: '功能请求 - 三角洲数据三角洲数据查询攻略站查询'
    }
  },
  {
    path: '/feature/submit',
    name: 'feature-submit',
    component: () => import('@/views/Feature/Submit.vue'),
    meta: {
      title: '提交功能请求 - 三角洲数据查询攻略站'
    }
  },
  {
    path: '/feature/:id',
    name: 'feature-detail',
    component: () => import('@/views/Feature/Detail.vue'),
    meta: {
      title: '功能请求详情 - 三角洲数据查询攻略站'
    },
    props: true
  },
  // 用户认证相关路由
  {
    path: '/auth',
    name: 'auth',
    component: () => import('@/views/Auth/LoginPage.vue'),
    meta: {
      title: '用户登录 - 三角洲数据查询攻略站',
      hideForAuth: true // 已登录用户隐藏
    }
  },
  {
    path: '/login',
    redirect: '/auth?tab=login'
  },
  {
    path: '/register',
    redirect: '/auth?tab=register'
  },
  {
    path: '/forgot-password',
    redirect: '/auth?tab=forgot'
  },
  // 用户中心相关路由
  {
    path: '/user',
    name: 'user',
    redirect: '/user/center',
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/user/center',
    name: 'user-center',
    component: () => import('@/views/User/UserCenter.vue'),
    meta: {
      title: '个人中心 - 三角洲数据查询攻略站',
      requiresAuth: true
    }
  },
  // 向后兼容的重定向路由
  {
    path: '/user/profile',
    redirect: '/user/center?tab=profile'
  },
  {
    path: '/user/settings',
    redirect: '/user/center?tab=settings'
  },
  {
    path: '/user/favorites',
    redirect: '/user/center?tab=favorites'
  },
  {
    path: '/user/achievements',
    redirect: '/user/center?tab=achievements'
  },
  {
    path: '/user/activities',
    redirect: '/user/center?tab=activities'
  },
  {
    path: '/update-log',
    name: 'update-log',
    component: () => import('@/views/UpdateLog.vue'),
    meta: {
      title: '更新日志 - 三角洲数据查询攻略站'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/NotFoundPage.vue'),
    meta: {
      title: '页面未找到 - 三角洲数据查询攻略站'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由前置守卫
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title as string
  }

  // 获取用户store
  const userStore = useUserStore()

  // 初始化用户状态（仅在首次访问时）
  if (!userStore.userInfo && userStore.token) {
    try {
      await userStore.initializeUser()
    } catch (error) {
      console.error('初始化用户状态失败:', error)
    }
  }

  // 检查需要登录的路由
  if (to.meta.requiresAuth) {
    if (!userStore.isLoggedIn) {
      // 未登录，重定向到登录页面，并保存原始路径
      next({
        path: '/auth',
        query: {
          redirect: to.fullPath,
          tab: 'login'
        }
      })
      return
    }
  }

  // 检查已登录用户不应访问的页面（如登录页）
  if (to.meta.hideForAuth && userStore.isLoggedIn) {
    // 已登录用户访问登录页，重定向到个人中心
    const redirect = to.query.redirect as string
    next(redirect || '/user/center')
    return
  }

  next()
})

export default router 